package com.team06.service.impl;
import com.team06.mapper.AirlHistoryMapper;
import com.team06.mapper.FlightUpdateMapper;
import com.team06.pojo.*;
import com.team06.service.FlightUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class FlightUpdateServiceImpl implements FlightUpdateService {
    @Autowired
    private FlightUpdateMapper flightUpdateMapper;
    @Autowired
    private AirlHistoryMapper airlHistoryMapper;
    @Override
    public void updatePlaneByFlno(String flno, String cfno, String cftp, String stat) {
        flightUpdateMapper.updatePlaneByFlno(flno, cfno, cftp, stat);
        System.out.println("通过 FLNO 更新航班飞机信息成功: " + flno);
    }
    //航班时间
    @Override
    public void updateFlightAndAirportInfo(String flno, String fide, String stat, String fptt, String fplt) {
        flightUpdateMapper.updateFlightBasicInfo(flno, fide, stat);
        flightUpdateMapper.updateAirportTimeInfo(fptt, fplt, flno); // 把 flno 当作查询条件传入
    }
    //机场信息
    @Override
    public void updateAirportInfo(String apotCode, String apotApat, String apotCnnm, String apotEnnm, String apotAiso, String apotApsn) {
        flightUpdateMapper.updateAirportInfo(apotCode, apotApat, apotCnnm, apotEnnm, apotAiso, apotApsn);
        System.out.println("机场信息已成功更新：APOT_CODE = " + apotCode);
    }
//    @Override
//    public void updateairlines(Airl airl) {
//        System.out.println("Received airl data: " + airl);
//        String oldAirlArptId = flightUpdateMapper.getOldAirlArptId(airl.getDfltFlid());
//        flightUpdateMapper.updateairlines(airl);
//        AirlHistory history = new AirlHistory();
//        history.setAirlId(airl.getDfltFlid());
//        history.setOldAirlArptId(oldAirlArptId);
//        history.setNewAirlArptId(airl.getAirlArptId());
//        history.setUpdatedBy("SYSTEM"); // 或者当前用户
//        // 插入变更历史
//        airlHistoryMapper.insertAirlHistory(history);
//    }
    @Override
    public void submitAirlChange(Airl airl) {
        if (airl == null || airl.getDfltFlid() == null) {
            throw new RuntimeException("必须提供有效的 DFLT_FLID");
        }
        System.out.println("Received airl data: " + airl);
        Airl currentAirl = flightUpdateMapper.getAirlWithPending(airl.getDfltFlid());
        if (currentAirl != null) {
            currentAirl.setPendingAirlArptId(airl.getAirlArptId()); // 写入待审核字段
            currentAirl.setApprovalStatus("PENDING");
            System.out.println("准备提交变更: " + currentAirl);
            flightUpdateMapper.submitAirlChangeForApproval(currentAirl);
            System.out.println("航线变更已提交，请等待审核");

            // 可选：记录历史变更
            AirlHistory history = new AirlHistory();
            history.setAirlId(airl.getDfltFlid());
            history.setOldAirlArptId(currentAirl.getAirlArptId());
            history.setNewAirlArptId(airl.getAirlArptId());
            history.setUpdatedBy("SYSTEM"); // 或者当前用户
            airlHistoryMapper.insertAirlHistory(history);
        }
    }
    @Override
    public void approveAirlChange(Integer dfltFlid, String recipientEmail) {
        Airl airl = flightUpdateMapper.getAirlWithPending(dfltFlid);
        if (airl == null || !"PENDING".equals(airl.getApprovalStatus())) {
            throw new RuntimeException("该记录无需审核或不存在");
        }

        airl.setAirlArptId(airl.getPendingAirlArptId()); // 将待审核值设为生效值
        airl.setApprovalStatus("APPROVED");
        airl.setPendingAirlArptId(null);

        flightUpdateMapper.approveAirlChange(airl);
        System.out.println("航线变更已审核通过并更新");
    }

    @Override
    public void rejectAirlChange(Integer dfltFlid) {
        Airl airl = flightUpdateMapper.getAirlWithPending(dfltFlid);
        if (airl == null || !"PENDING".equals(airl.getApprovalStatus())) {
            throw new RuntimeException("该记录无需审核或不存在");
        }

        // 更新审批状态为 REJECTED，并清空待审核字段
        airl.setApprovalStatus("REJECTED");
        airl.setPendingAirlArptId(null);

        flightUpdateMapper.rejectAirlChange(airl);
        System.out.println("航线变更已审核拒绝");
    }
    @Override
    public void updateBllsBeltByDfltFlidWithParams(String dfltFlid,  String btno, String code, String estr, String eend, String rstr, String rend) {
        flightUpdateMapper.updateBllsBeltFieldsByDfltFlid(dfltFlid,  btno, code, estr, eend, rstr, rend);
        System.out.println("行李转盘信息已更新：DFLT_FLID = " + dfltFlid);
    }

    @Override
    public void updateGtlGateByDfltFlidWithParams(String dfltFlid, String gtno, String code, String btsc, String rstr, String rend) {
        flightUpdateMapper.updateGtlGateFieldsByDfltFlid(dfltFlid, gtno, code, btsc, rstr, rend);
        System.out.println("登机口信息已更新：DFLT_FLID = " + dfltFlid);
    }



}
